home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 1 Issue 2
/
PDCD-1 - Issue 02.iso
/
_utilities
/
utilities
/
004
/
gstobject
/
set
/
Example
/
c
/
main
< prev
Wrap
Text File
|
1994-09-24
|
3KB
|
117 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "C:h.kernel"
#include "^.151.100.set"
void initAdhesive(void); /* defined in Adh.c */
void mySetCallProc(void *element, void *private)
{
printf("%s: %c\n",(char *)private,*((char *)element));
}
int main()
{
char *a,*b,*c,*d; /* elements */
Set A,B,C,D,X,Y,Z; /* some sets */
/* intialise Adhesive stuff */
initAdhesive();
/* We shall create 4 elements a,b,c,d. Each element will
one character so that we (the humans) can easily see which
element is which.
Each of these elements is in a set of 1. Note that the
element (not the Set) is returned by set_Make();
*/
a = set_Make(sizeof(char));
b = set_Make(sizeof(char));
c = set_Make(sizeof(char));
d = set_Make(sizeof(char));
if (!a || !b || !c || !d) {
printf("out of memory\n");
exit(EXIT_FAILURE);
}
/* We shall assign the letters a-d to these elements */
*a='a';
*b='b';
*c='c';
*d='d';
/* We shall now find out the set each element is in.
(This will also possibly rearrange internal data to
optimise performance - path compression, in this case
though there's nothing to optimise.)
*/
A = set_Find(a);
B = set_Find(b);
C = set_Find(c);
D = set_Find(d);
/* We shall now output all the elements in set A. */
set_Call(A,mySetCallProc,"set A");
/* We shall now let X = A union B. This means set A and B
are no longer accessible. (In fact C=A or C=B but we don't
really care about this - the header file gives more details
about this.)
*/
X = set_Union(A,B);
/* We shall do something similar for Y = C union D. */
Y = set_Union(C,D);
/* check that a and b are in the same set */
if (set_Find(a)==set_Find(b)) {
printf("a and b are in the same set\n");
} else {
printf("a and b are not in the same set\n");
}
/* we shall now join set X and Y to make Z */
Z = set_Union(X,Y);
/* we shall assign the value associated with set Z to point to a string */
set_SetValue(Z,"this is a test");
/* We shall check the a and d are in the same set. If we were
to do this same operation many times we would in fact find that
the first time is slower than the others. This is because the
first time round path compression is done whilst searching the
data structure. If you don't understand this you could look at
reference 1 (see the set.h file) - or you could ignore this fact.
In a much larger set this path compression will enable calls
to set_Find() on the same set (ie Z) to occur much faster.
*/
if (set_Find(a)==set_Find(d)) {
printf("a and d are in the same set\n");
} else {
printf("a and d are not in the same set\n");
}
/* We now output all elements in the set Z. */
set_Call(Z,mySetCallProc,"set Z");
/* we shall output the string which Z's value points to */
printf("%s\n",set_GetValue(Z));
/* finally we can dispose of set Z, though this isn't
strictly important as our program is about to terminate
anyway
*/
set_Dispose(Z);
return EXIT_SUCCESS;
}